//
//  LoginSetViewController.m
//  ProjectTemplet
//
//  Created by HouKavin on 16/2/5.
//  Copyright © 2016年 HouKavin. All rights reserved.
//

#import "LoginSetViewController.h"
#import "GlobalReference.h"

@implementation LoginSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavLeftBtn];
    [self loadLocalDefaults];
}

-(void)setNavLeftBtn{
    //设置导航栏左侧按钮及事件
    UIButton *leftbutton=[UIButton buttonWithType:UIButtonTypeCustom];
    leftbutton.frame=CGRectMake(0, 0, 20, 20);
    [leftbutton setImage:[UIImage imageNamed:@"left_icon_blue"] forState:UIControlStateNormal];
    [leftbutton addTarget:self action:@selector(goBackLastView:) forControlEvents:UIControlEventTouchDown];
    UIBarButtonItem *leftbar=[[UIBarButtonItem alloc] initWithCustomView:leftbutton];
    [self.navigationItem setLeftBarButtonItem:leftbar animated:YES];
}

-(void)goBackLastView:(id)sender{
    if (self.urlAddressTfd.text.length != 0) {
        [AppInfo sharedInstance].baseURL = self.urlAddressTfd.text;
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self goBackAction];
    }
    
}

-(void)goBackAction{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"您还未设置服务器地址,确定离开该页面？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    [alertController addAction:cancelAction];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [alertController addAction:sureAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

//读取本地储存的服务器地址
- (void)loadLocalDefaults{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //获取ip地址信息
    NSString *baseUrl = [defaults objectForKey:@"baseUrl"];
    NSLog(@"baseUrl %@",baseUrl);
    if(baseUrl.length != 0) {
        self.urlAddressTfd.text = baseUrl;
        [AppInfo sharedInstance].baseURL = baseUrl;
    }
}

@end
